package xq.gwt.mvc.model;

public class IntegerPropertyModel extends AbstractPropertyModel {
		
	private static final long serialVersionUID = 4388309902464132668L;
	private Integer value;

	public Integer getValue() {
		return value;
	}

	public void setValue(Integer value) {
		Integer oldValue = this.value;
		this.value = value;
		setHasError(false);
		notifyPropertyChanged(oldValue, value);
	}

	@Override
	public String getText() {
		if(getValue() == null)
			return null;
		return String.valueOf(getValue());		
	}

	@Override
	public void setText(String text) throws ConversionException{
		setHasError(false);
		if((text == null) ||(text.equals(""))){
			setValue(null);
			return;
		}
		Integer intVal;
		try {
			intVal = Integer.parseInt(text);
		} catch (Exception e) {
			ConversionException ce = new ConversionException(e.getLocalizedMessage());
			ce.initCause(e);
			setHasError(true);
			throw ce;
		}
		
		setValue(intVal);
		
	}
	
	public Object getObjectValue() {		
		return getValue();
	}
	

	public Class getPropertyType() {		
		return Integer.class;
	}
}
